package example.com.terry.servlet;

import example.com.terry.utils.CookieUtils;
import example.com.terry.utils.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * ClassName: QueryWorkServlet <br/>
 * Description: 作业增加接口 <br/>
 * Date: 2020/5/21 下午5:27 <br/>
 * <br/>
 *
 * @author ls
 * <p/>
 * 修改记录
 * @version 1.0<br />
 */
@WebServlet("/insertWorkServlet")
public class InsertWorkServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (CookieUtils.isSign(req)) {
            String workText = req.getParameter("workText");
            String workDate = req.getParameter("workDate");
            String type = req.getParameter("type");
            String id = req.getParameter("id");

            if (id != null) {
                String sql = "update  tb_work set work_text = '" + workText + "' , work_date = '"+workDate+"',type = "+type+" where id = " + id;
                boolean init = JDBCUtils.init(sql);
                if (init) {
                    resp.getWriter().println("修改作业记录成功");
                } else {
                    resp.getWriter().println("修改作业记录失败");
                }
            }else {
                String sql = "insert into tb_work(work_text,work_date,type,user_id) values('" + workText + "',now(),'0','" + CookieUtils.getUser(req) + "')";
                boolean init = JDBCUtils.init(sql);
                if (init) {
                    resp.getWriter().println("增加作业记录成功");
                } else {
                    resp.getWriter().println("增加作业记录失败");
                }
            }

        }
    }
}
